package org.xjh.selectimagevideo;

import android.content.Intent;
import android.net.Uri;

import android.os.Bundle;
import android.os.Environment;

import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Toast;


import org.xjh.selectimagevideo.listener.GetFilePathListener;


import java.io.File;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by jianhongxu on 2017/6/18.
 */

public class SelectImageVideoAct extends AppCompatActivity implements GetFilePathListener {

    private static final int REQUEST_CAMERA = 0x001;
    public static final String KEY_RESULT = "Image_Video_Result";
    public static final String KEY_TYPE = "Image_Or_Video";
    public static final String KEY_CAMERA_SHOW = "Camera_Show";
    public static final String KEY_SELECT_NUM = "Select_Num";

    private SelectImageVideoView selectImageVideoView;

    private ArrayList<String> mSelectList = new ArrayList<>();

    private int typeData;//查询数据类型
    private boolean cameraShow;//是否显示照相
    private int selectNum;//选择数量
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_selectimagevideo);


        selectImageVideoView = (SelectImageVideoView) findViewById(R.id.siv_view);

        selectImageVideoView.addGetFilePathListener(this);


        typeData = getIntent().getIntExtra(KEY_TYPE,SelectImageVideoView.TYPE_IMAGE);//默认选择头像
        cameraShow = getIntent().getBooleanExtra(KEY_CAMERA_SHOW,true);//默认显示
        selectNum = getIntent().getIntExtra(KEY_SELECT_NUM,1)<=0?1:getIntent().getIntExtra(KEY_SELECT_NUM,1);//选择数量

        selectImageVideoView.setData(typeData,selectNum,cameraShow);
    }



    @Override
    public void openCamera() {
        // 创建临时相机文件
        selectImageVideoView.setFileCamera(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), String.valueOf(new Date().getTime()) + ".jpg"));
        // 跳转到系统照相机
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            // 设置系统相机拍照后的输出路径
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(
                    selectImageVideoView.getFileCamera()
            ));
            startActivityForResult(cameraIntent, REQUEST_CAMERA);
        } else {
            Toast.makeText(getApplicationContext(),
                    "没有找到摄像头", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getFilePath(String path) {
        if (!mSelectList.contains(path)) {
            mSelectList.add(path);
        }
        returnData();
    }

    @Override
    public void getFilelistPath(ArrayList<String> path) {
        mSelectList.clear();
        mSelectList=  path;
        returnData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CAMERA://照相
                    if (selectImageVideoView.getFileCamera().exists()) {
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + selectImageVideoView.getFileCamera().getAbsolutePath())));
                        if (!mSelectList.contains(selectImageVideoView.getFileCamera().getAbsolutePath())) {
                            mSelectList.add(selectImageVideoView.getFileCamera().getAbsolutePath());
                        }
                        returnData();
                    }
                    break;
            }
        }
    }

    private void returnData() {
        Intent data = new Intent();
        data.putStringArrayListExtra(KEY_RESULT,mSelectList);
        setResult(RESULT_OK,data);
        finish();
//        // 返回已选择的图片数据
//        Intent data = new Intent();
//        data.putStringArrayListExtra(KEY_RESULT, mSelectList);
//        setResult(RESULT_OK, data);
//        finish();
    }
}
